//
//  ViewController.m
//  相册
//
//  Created by 黄世光 on 16/3/23.
//  Copyright © 2016年 黄世光. All rights reserved.
//

#import "ViewController.h"
#import "KKImagePickerController.h"
@interface ViewController ()<KKImagePickerControllerDelegate>
{
    UIAlertController *alertController;
    UIImageView *imageView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, 100, 100)];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    KKImagePickerController *imagePickerController = [KKImagePickerController shareImagePickerManager];
    imagePickerController.delegate = self;
    alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //添加button
    [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //处理点击拍照
        [imagePickerController takePicture];
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"从相册里选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //处理点击从相册选取
         [imagePickerController getPhotosFromAlbum];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
//    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)btn{
    [self presentViewController:alertController animated:YES completion:nil];
}
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//    
//    if (buttonIndex >1) {
//        return;
//    }
//    
//    NSInteger maxCount = 4 - (actionSheet.tag == NSIntegerMax ? _update_credFilesArray.count : _update_proFilesArray.count);
//    
//    if (maxCount <= 0) {
//        [[KKResultAlertView sharedInstance] alertViewShowMessage: @"数量限制" andWithSuccessful: false andWithCallBack: nil];
//        return;
//    }
//    
//    KKImagePickerController *imagePickerController = [KKImagePickerController shareImagePickerManager];
//    imagePickerController.delegate = self;
//    imagePickerController.tag = actionSheet.tag;
//    imagePickerController.maxCount = maxCount;
//    
//    if (buttonIndex == 0) {
//        [imagePickerController takePicture];
//    }
//    else if (buttonIndex == 1) {
//        [imagePickerController getPhotosFromAlbum];
//    }
//}

#pragma mark - KKImagePickerController methods

- (void)imagePickerController:(KKImagePickerController *)imagePickerController didSelectedPhotos:(NSArray *)assetItems {
    
        NSLog(@"assetItems = %@", assetItems);
    
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
     id object = assetItems[0];
    if ([object isKindOfClass: [ALAsset class]]) {
        ALAsset *asset = (ALAsset*)object;
        UIImage *image = [KKImagePickerController fullImageFromAsset: asset];//不缩放   
        imageView.image = image;
        //等比例缩放
        CGFloat height = self.view.frame.size.width/image.size.width * image.size.height;

        imageView.frame = CGRectMake(0, 0,self.view.frame.size.width,height);

    }
    
    [self.view addSubview:imageView];
//    if (imagePickerController.tag == NSIntegerMax) {
//        [_update_credFilesArray addObjectsFromArray: assetItems];
//        [_selectedCell updateCredFileItems: _update_credFilesArray];
//    }
//    else {
//        [_update_proFilesArray addObjectsFromArray: assetItems];
//        [_selectedCell updateCredFileItems: _update_proFilesArray];
//    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
